<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWPProductCooker
 *
 * @author jianfeng
 */
class BWPProductCooker extends BAEventCooker {

    // 手机端
    /**
     * 显示手机端首页
     */
    public function onShowHome() {
        $this->updateControl->addRenderView('home', 'client');
    }
    
    /**
     * 展示某一分类下所有的产品列表
     */
    public function onShowCategoryProductList() {
        $categoryName = $this->activeLink->getLinkVariable('categoryName');
        
        //获取所有的产品
        $event = $this->getCurrentEvent();
        $productTable = new BPProductSheet($event);
        $productView = $productTable->getView();
        
        //用于存储符合该分类的产品
        $finalProductView = array();
        
        foreach ($productView as $productList) {
            //获得该产品的所有的分类
            $categoryView = BACategory::getMemberCategoryView($productList);
            foreach ($categoryView as $categoryList) {
                $categoryId = $categoryList->getItem('categoryId')->getValue();
                //根据分类的id进而获取分类的名称
                $category = new BACategory($categoryId);
                $categoryFullName = $category->getCategoryFullName();
                //把分类名与用户选择的分类同名的产品保存起来
                if($categoryName == $categoryFullName) {
                    array_push($finalProductView, $productList);
                    break;
                }
            }
        }
        
        $this->updateControl->addRenderView('productList', 'products', array('productView' => $finalProductView));
    }
    
    
    
    // 电脑端
    public function onShowProductList($owner = NULL) {
        //$venderId = $this->activeLink->getLinkVariable('venderId');
        //if ($owner == NULL) {
        //$owner = $this->getOwnerFromActiveLinks();
        //}
        //if ($owner == NULL) {
        //$owner = Blyn::app()->getActiveSpace();
        //}

        $event = $this->getCurrentEvent();

        $productTable = new BPProductSheet($event);
        $productView = $productTable->getView();

        $this->updateControl->addRenderView('productList', 'product_list', array(
            //'venderId'=>$venderId,
            'productView' => $productView));
    }
    
    public function showProductList($owner = NULL) {
        //$venderId = $this->activeLink->getLinkVariable('venderId');
        //if ($owner == NULL) {
        //$owner = $this->getOwnerFromActiveLinks();
        //}
        //if ($owner == NULL) {
        //$owner = Blyn::app()->getActiveSpace();
        //}

        $event = $this->getCurrentEvent();

        $productTable = new BPProductSheet($event);
        $productView = $productTable->getView();

        $this->updateControl->renderView('product_list', array(
            //'venderId'=>$venderId,
            'productView' => $productView));
    }

    public function onShowAddProduct($owner = NULL) {

        $event = $this->getCurrentEvent();
        $eventId = $event->getId();
        $productId = $this->activeLink->getLinkVariable('productId');

        $model = new BDynamicFormModel();
        $productNameField = 'media_name_add_product';
        $productDescriptionField = 'media_description_add_product';
        $productAliasField = 'media_alias_add_product';

        $model->addField($productNameField);
        $model->addField($productDescriptionField);
        $model->addField($productAliasField);

        $productTable = new BPProductSheet();

        if ($productId != NULL && $productId > 0) {
            $eventTable = new BAEventSheet();
            $event = $eventTable->getListById($eventId);

            $product = $productTable->getListById($productId);

            //reset list name
            if ($product->getName() == 'list_' . Blyn::app()->getActiveOperation()->getId() . '_' . Blyn::app()->getCurrentUser()->getId()) {
                $pId = $product->getId();
                $product->name = 'list' . $pId;
                $product->save();
            }

            //将已经存在的活动数据放进MODEL里面
            $model->$productNameField = $product->getItem('name')->getValue();
            if ($product->getItem('description') != NULL)
                $model->$productDescriptionField = $product->getItem('description')->getValue();
            if ($product->getItem('alias') != NULL)
                $model->$productAliasField = $product->getItem('alias')->getValue();

            //add attribute data into model
            //$attrributeControl = new BUIAttribute();
            //$attributeTable = new BPProductSheet('productAttribute', $product);
            //$attrributeControl->populateModelFromAttribute($model, $product, $attributeTable, 'add_product');
        }else {
            //below code create a temporary product
            $listId = 'list_' . Blyn::app()->getActiveOperation()->getId() . '_' . Blyn::app()->getCurrentUser()->getId();
            $product = $productTable->getList(array('name' => $listId));
            if ($product == NULL) {
                $product = $productTable->getNewList(NULL, $listId);
            }
        }

        $productId = $product->getId();

        $this->updateControl->addRenderView('productAdd', 'product_add', array(
            'model' => $model,
            'productNameField' => $productNameField,
            'productAliasField' => $productAliasField,
            'productDescriptionField' => $productDescriptionField,
            'product' => $product,
            'productId' => $productId));
    }

    public function onAddProduct($data = NULL) {

        $productId = $this->activeLink->getLinkVariable('productId');

        $mediaList = $this->postManager->populateMediaListFromPostData($this->postData, 'add_product');

        $itemData = array();
        foreach ($mediaList as $media) {
            $name = $media->name;
            $value = $media->content;
            if ($value != NULL || $value != '') {
                $itemData[$name] = $value;
            }
        }

        $event = $this->getCurrentEvent();
        $productTable = new BPProductSheet($event);
        if ($productId != NULL && $productId > 0) {
            $product = $productTable->getListById($productId);
            //reset product name
            if ($product->getName() != 'list' . $productId) {
                $product->name = 'list' . $productId;
                $product->setOwner($event);
                $product->save();
            }
        } else {
            $product = $productTable->getNewList();
        }
        //save product
        $product->addItems($itemData);
        //reset product name

        $this->onShowProductList();
    }

}
